package io.gitee.thinkbungee.component;

import io.gitee.thinkbungee.bean.ResponseResult;
import io.gitee.thinkbungee.component.push.service.MailPushService;
import io.gitee.thinkbungee.constant.SatelliteConstant;
import io.gitee.thinkbungee.utils.ApplicationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.text.WordUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenyichang
 * @since 2022/5/30
 */
@RestController
@RequestMapping("/component")
@Slf4j
public class ComponentController {
    /**
     * 当配置文件中
     * {@code SatelliteConstant.Component.PREFIX + SatelliteConstant.Component.MAIL + SatelliteConstant.Component
     * .ENABLE=true} 才能自动注入,否则会报该bean没有被加载
     */
    @GetMapping("/mail")
    public ResponseResult mailTest() {
        String mailProperty =
                SatelliteConstant.Component.PREFIX + SatelliteConstant.Component.MAIL + SatelliteConstant.Component.ENABLE;
        String property = ApplicationContextUtils.applicationContext.getEnvironment().getProperty(mailProperty);
        String msg = String.format(mailProperty + "设置的值：%s；所以%s是否存在:%s", property,
                MailPushService.class.getSimpleName(),
                //首字母小写
                ApplicationContextUtils.containsBean(WordUtils.uncapitalize(MailPushService.class.getSimpleName())));
        //如果存在mail服务，发送邮件
        if (ApplicationContextUtils.containsBean(WordUtils.uncapitalize(MailPushService.class.getSimpleName()))) {
            MailPushService mailPushService = ApplicationContextUtils.getBean(MailPushService.class);
            mailPushService.sendSimpleMail("820117089@qq.com", "测试", "哈哈");
        }
        return ResponseResult.success(msg);
    }
}
